package com.winhxd.b2c.admin.module.system.controller;


import com.qcloud.Utilities.Json.JSONObject;
import com.winhxd.b2c.admin.common.security.annotation.CheckPermission;
import com.winhxd.b2c.admin.utils.QcloudUtils;
import com.winhxd.b2c.admin.utils.Signature;
import com.winhxd.b2c.admin.utils.VodUpload;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.common.model.BaseImageFile;
import com.winhxd.b2c.common.domain.system.security.enums.PermissionEnum;
import com.winhxd.b2c.common.domain.system.video.condition.VideoCondition;
import com.winhxd.b2c.common.domain.system.video.vo.MediaVO;
import com.winhxd.b2c.common.domain.system.video.vo.VideoBasicInfoVO;
import com.winhxd.b2c.common.util.ImageUploadUtil;
import com.winhxd.b2c.common.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Api(tags = "文件上传管理")
@RestController
@RequestMapping("/upload")
public class UploadController {
    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);

    @Autowired
    private ImageUploadUtil imageUploadUtil;

    @Autowired
    private QcloudUtils qcloudUtils;

//    @CheckPermission({PermissionEnum.AUTHENTICATED})
    @ApiOperation("OSS上传图片")
    @PostMapping(value = "/3070/v1/oss/picture",consumes = "multipart/*",headers = "content-type=multipart/form-data")
    public ResponseResult<BaseImageFile> uploadPicture(MultipartHttpServletRequest request) throws IOException,Exception {
        MultipartFile file = request.getFile("file");
        ResponseResult<BaseImageFile> result = new ResponseResult<>();
        BaseImageFile baseImageFile = imageUploadUtil.uploadImage2Oss(file);
        result.setData(baseImageFile);
        return result;
    }

    /**
     * @description 上传视频
     * @auther: yuluyuan
     * @date: 2019/1/5 18:43
     * @param: [condition, videoFile, model]
     * @return: java.lang.String
     */
    @CheckPermission({PermissionEnum.AUTHENTICATED})
    @PostMapping(value = "/3071/v1/qcloud/video",consumes = "multipart/*",headers = "content-type=multipart/form-data")
    public ResponseResult<MediaVO> upload(@RequestParam(required=false) MultipartFile mediaFile, Model model) {

        ResponseResult<MediaVO> result = new ResponseResult<>();
        //上传视频到腾讯云
        MediaVO vo = qcloudUtils.uploadVideo(mediaFile, model);

        if (!"0".equals(vo.getRetCode())){
            logger.info("视频上传失败！");
        }
        result.setData(vo);
        return result;

    }

    /**
     * @description 获取签名
     * @auther: yuluyuan
     * @date: 2019/1/5 18:43
     * @param: [condition, videoFile, model]
     * @return: java.lang.String
     */
    @CheckPermission({PermissionEnum.AUTHENTICATED})
    @PostMapping(value = "/3072/v1/qcloud/getSignature")
    public ResponseResult<String> getSignature() {

        ResponseResult<String> result = new ResponseResult<>();
        logger.info("开始获取签名");

        //TODO yuluyuan 密钥需要配置在config
        Signature sign = new Signature();
        sign.setSecretId("AKIDkVVNeyt1NVavQYafXbQMnhoYVFcWLthi");
        sign.setSecretKey("l1ZUmey82VzvMU5PJHECW6Cqi21PNhWC");
        sign.setCurrentTime(System.currentTimeMillis() / 1000);
        sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));
        sign.setSignValidDuration(3600 * 24 * 2);

        try {
            String signature = sign.getUploadSignature();
            result.setData(signature);
            logger.info("signature : " + signature);
        } catch (Exception e) {
            logger.info("获取签名失败",e);
        }

        return result;

    }

    /**
     * @description 获取视频信息
     * @auther: yuluyuan
     * @date: 2019/1/5 18:43
     * @param: [condition, videoFile, model]
     * @return: java.lang.String
     */
    @CheckPermission({PermissionEnum.AUTHENTICATED})
    @PostMapping(value = "/3073/v1/qcloud/getVideoInfo")
    public ResponseResult<VideoBasicInfoVO> getVideoInfo(@RequestBody VideoCondition condition) {

        ResponseResult<VideoBasicInfoVO> result = new ResponseResult<>();
        logger.info("获取视频信息 参数：" + JsonUtil.toJSONString(condition));

        JSONObject json_result = VodUpload.getVideoInfo(condition.getFileId());
        Object basicInfo = json_result.get("basicInfo");
        VideoBasicInfoVO vo = JsonUtil.parseJSONObject(basicInfo.toString(), VideoBasicInfoVO.class);
        result.setData(vo);
        return result;

    }


}
